home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Utility Library / 3DMF2PICT ƒ / 3dmfToPict.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  7.7 KB  |  319 lines  |  [TEXT/CWIE]

  1. /*
  2.  * read a 3dmf file, write a pict out, for the utility library
  3.  *
  4.  * It uses the QuickDraw 3D utility library
  5.  *
  6.  * Nick Thompson, nickt@apple.com
  7.  * Send bugs to devsupport@apple.com
  8.  *
  9.  */
  10.  
  11. #include <stdlib.h>
  12.  
  13. #include "Q3UL.h"
  14.  
  15. #include "QD3D.h"
  16. #include "QD3DGeometry.h"
  17. #include "QD3DGroup.h"
  18. #include "QD3DMath.h"
  19. #include "QD3DShader.h"
  20. #include "QD3DTransform.h"
  21.  
  22.  
  23. #define        MYIDTYPE        theWindowRef
  24.  
  25. /*------------------------------------------------------------------------------
  26.  * types.
  27.  */ 
  28.  
  29. typedef struct DocumentRec {
  30.     TQ3GroupObject    fModel ;    /* some geometry to draw         */
  31.     TQ3Matrix4x4    fRotation;    /* the transform for the model  */
  32. } DocumentRec ;
  33.  
  34. void            MyInitWindowData( TQ3UL_WindowRef theWindowRef ) ;
  35. TQ3GroupObject    MyNewModel( void ) ;
  36.  
  37. void    MyCloseHandler( TQ3UL_WindowRef theWindowRef ) ;
  38. void     MyRedrawHandler( TQ3UL_WindowRef windowID, TQ3ViewObject viewRef ) ;
  39. void     MyIdleHandler( TQ3UL_WindowRef windowID ) ;
  40.  
  41. /*------------------------------------------------------------------------------
  42.  * 
  43.  */ 
  44.  
  45. int main(void) 
  46. {
  47.     if( Q3UL_Initialize() == kQ3Success )
  48.     {
  49.         /* make a new window */
  50.         TQ3UL_WindowRef         theWindowRef ;
  51.         
  52.         theWindowRef = Q3UL_NewWindow( 400, 400 ) ;
  53.         
  54.         /* initlialize the window data */
  55.         MyInitWindowData( theWindowRef ) ;
  56.         
  57.         /* call the library main event loop */
  58.         Q3UL_MainEventLoop() ;
  59.     }
  60. }
  61.  
  62. /*------------------------------------------------------------------------------
  63.  * 
  64.  */ 
  65.  
  66. void    MyInitWindowData(TQ3UL_WindowRef theWindowRef)
  67. {
  68.     DocumentRec        *myDocumentRec = (DocumentRec *)malloc(sizeof(DocumentRec)) ;
  69.     
  70.     /* the identifier is not really used in this app, in a larger app, it would probably
  71.      * be used to switch between window/document types 
  72.      */
  73.     long            myIdentifier = MYIDTYPE ;
  74.  
  75.     myDocumentRec->fModel = MyNewModel() ;                /* the main display group */
  76.     Q3Matrix4x4_SetIdentity(&myDocumentRec->fRotation);    /* set to the identity matrix */
  77.         
  78.     /* set the identifier for this app and stash the document record away */
  79.     Q3UL_SetPrivType( theWindowRef, myIdentifier ) ;
  80.     Q3UL_SetPrivData( theWindowRef, (void *)myDocumentRec ) ;    
  81.     
  82.     /* install handlers for the window */
  83.     Q3UL_RegisterRedraw( theWindowRef, MyRedrawHandler ) ;
  84.     Q3UL_RegisterIdle( theWindowRef, MyIdleHandler ) ;
  85.     Q3UL_RegisterCloseWindowHandler( theWindowRef, MyCloseHandler ) ;
  86.     
  87. }
  88.  
  89.  
  90. /*------------------------------------------------------------------------------
  91.  * 
  92.  */ 
  93.  
  94. void    MyCloseHandler(TQ3UL_WindowRef theWindowRef)
  95. {
  96.     unsigned long            thisType ;
  97.     
  98.     /*
  99.      * get the private data from the window reference
  100.      * first ensuring that the document is the right type.
  101.      */
  102.      
  103.     thisType = Q3UL_GetPrivType( theWindowRef ) ;
  104.     
  105.     if( thisType == MYIDTYPE )
  106.     {
  107.         DocumentRec        *thisDocument ;
  108.         thisDocument = (DocumentRec *)Q3UL_GetPrivData( theWindowRef ) ;    
  109.         
  110.         if( thisDocument->fModel != NULL )
  111.             Q3Object_Dispose( thisDocument->fModel ) ;
  112.         
  113.         if( thisDocument != NULL )    
  114.             free( thisDocument ) ;
  115.         
  116.         /* the user clicked in the close box,
  117.          * quit and bail 
  118.          */
  119.     }
  120.     
  121.     Q3UL_DestroyWindow( theWindowRef ) ;
  122.     Q3UL_Terminate() ;
  123. }
  124.  
  125. /*------------------------------------------------------------------------------
  126.  * .
  127.  */ 
  128.  
  129. void MyRedrawHandler( TQ3UL_WindowRef windowID, TQ3ViewObject viewRef ) 
  130. {
  131.     DocumentRec        *thisDocument ;
  132.     
  133.     thisDocument = (DocumentRec *)Q3UL_GetPrivData( windowID ) ;    
  134.     if( thisDocument != NULL )
  135.     {
  136.         Q3View_StartRendering( viewRef );
  137.         do {
  138.             Q3MatrixTransform_Submit( &thisDocument->fRotation, viewRef );
  139.             Q3DisplayGroup_Submit( thisDocument->fModel, viewRef );
  140.         } while (Q3View_EndRendering( viewRef ) == kQ3ViewStatusRetraverse );
  141.     }
  142. }
  143.  
  144. /*------------------------------------------------------------------------------
  145.  * 
  146.  */ 
  147.  
  148. void MyIdleHandler( 
  149.     TQ3UL_WindowRef     windowID )  
  150. {
  151.     TQ3Matrix4x4    tmp;
  152.     DocumentRec        *thisDocument ;
  153.     
  154.     thisDocument = (DocumentRec *)Q3UL_GetPrivData( windowID ) ;    
  155.     if( thisDocument != NULL )
  156.     {        
  157.         Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  158.         Q3Matrix4x4_Multiply(&thisDocument->fRotation, &tmp, &thisDocument->fRotation) ;
  159.     }
  160.     Q3UL_RedrawWindow( windowID ) ;
  161.  
  162. }
  163.  
  164.  
  165. /*------------------------------------------------------------------------------
  166.  * 
  167.  */ 
  168.  
  169.  
  170. void MyColorBoxFaces( 
  171.     TQ3BoxData         *myBoxData )
  172. {
  173.     TQ3ColorRGB                faceColor ;
  174.     short                     face ;
  175.     
  176.     /* sanity check - you need to have set up 
  177.      * the face attribute set for the box data 
  178.      * before calling this.
  179.      */
  180.     
  181.     if( myBoxData->faceAttributeSet == NULL )
  182.         return ;
  183.         
  184.     /* make each face of a box a different color */
  185.     
  186.     for( face = 0; face < 6; face++) {
  187.         
  188.         myBoxData->faceAttributeSet[face] = Q3AttributeSet_New();
  189.         switch( face ) {
  190.             case 0:
  191.                 faceColor.r = 1.0;
  192.                 faceColor.g = 0.0;
  193.                 faceColor.b = 0.0;
  194.                 break;
  195.             
  196.             case 1:
  197.                 faceColor.r = 0.0;
  198.                 faceColor.g = 1.0;
  199.                 faceColor.b = 0.0;
  200.                 break;
  201.             
  202.             case 2:
  203.                 faceColor.r = 0.0;
  204.                 faceColor.g = 0.0;
  205.                 faceColor.b = 1.0;
  206.                 break;
  207.             
  208.             case 3:
  209.                 faceColor.r = 1.0;
  210.                 faceColor.g = 1.0;
  211.                 faceColor.b = 0.0;
  212.                 break;
  213.             
  214.             case 4:
  215.                 faceColor.r = 1.0;
  216.                 faceColor.g = 0.0;
  217.                 faceColor.b = 1.0;
  218.                 break;
  219.             
  220.             case 5:
  221.                 faceColor.r = 0.0;
  222.                 faceColor.g = 1.0;
  223.                 faceColor.b = 1.0;
  224.                 break;
  225.         }
  226.         Q3AttributeSet_Add(myBoxData->faceAttributeSet[face], 
  227.                     kQ3AttributeTypeDiffuseColor, &faceColor);
  228.     }
  229. }
  230.  
  231. /*------------------------------------------------------------------------------
  232.  * 
  233.  */ 
  234.  
  235. TQ3GroupPosition MyAddTransformedObjectToGroup( 
  236.     TQ3GroupObject        theGroup, 
  237.     TQ3Object            theObject, 
  238.     TQ3Vector3D            *translation )
  239. {
  240.     TQ3TransformObject    transform;
  241.  
  242.     transform = Q3TranslateTransform_New(translation);
  243.     Q3Group_AddObject(theGroup, transform);    
  244.     Q3Object_Dispose(transform);
  245.     return Q3Group_AddObject(theGroup, theObject);    
  246. }
  247.  
  248. /*------------------------------------------------------------------------------
  249.  * 
  250.  */ 
  251.  
  252. TQ3GroupObject MyNewModel( void )
  253. {
  254.     TQ3GroupObject            myGroup = NULL;
  255.     TQ3GeometryObject        myBox;
  256.     TQ3BoxData                myBoxData;
  257.     TQ3ShaderObject            myIlluminationShader ;
  258.     TQ3Vector3D                translation;
  259.     
  260.     TQ3SetObject                faces[6] ;
  261.     short                    face ;
  262.             
  263.     /* Create a group for the complete model.
  264.      * do not use Q3OrderedDisplayGroup_New since in this
  265.      * type of group all of the translations are applied before
  266.      * the objects in the group are drawn, in this instance we 
  267.      * dont want this.
  268.      */
  269.     if ((myGroup = Q3DisplayGroup_New()) != NULL ) {
  270.             
  271.         /* Define a shading type for the group
  272.          * and add the shader to the group
  273.          */
  274.         myIlluminationShader = Q3PhongIllumination_New();
  275.         Q3Group_AddObject(myGroup, myIlluminationShader);
  276.  
  277.         /* set up the colored faces for the box data */
  278.         myBoxData.faceAttributeSet = faces;
  279.         myBoxData.boxAttributeSet = nil;
  280.         MyColorBoxFaces( &myBoxData ) ;
  281.         
  282.         /* create the box itself */
  283.         Q3Point3D_Set(&myBoxData.origin, 0, 0, 0);
  284.         Q3Vector3D_Set(&myBoxData.orientation, 0, 1, 0);
  285.         Q3Vector3D_Set(&myBoxData.majorAxis, 0, 0, 1);    
  286.         Q3Vector3D_Set(&myBoxData.minorAxis, 1, 0, 0);    
  287.         myBox = Q3Box_New(&myBoxData);
  288.         
  289.         /* put four copies of the box into the group, each one with its own translation */
  290.         translation.x = 0;translation.y = 0;translation.z = 0;
  291.         MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
  292.         
  293.         translation.x = 2;translation.y = 0;translation.z = 0;
  294.         MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
  295.         
  296.         translation.x = 0;translation.y = 0;translation.z = -2;
  297.         MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
  298.         
  299.         translation.x = -2;translation.y = 0;translation.z = 0;
  300.         MyAddTransformedObjectToGroup( myGroup, myBox, &translation ) ;
  301.     }
  302.     
  303.     /* dispose of the objects we created here */
  304.     if( myIlluminationShader ) 
  305.         Q3Object_Dispose(myIlluminationShader);    
  306.             
  307.     for( face = 0; face < 6; face++) {
  308.         if( myBoxData.faceAttributeSet[face] != NULL )
  309.             Q3Object_Dispose(myBoxData.faceAttributeSet[face]);
  310.     }
  311.     
  312.     if( myBox ) 
  313.         Q3Object_Dispose( myBox );
  314.     
  315.     return ( myGroup );
  316. }
  317.  
  318.  
  319.